﻿using HoverTree.IDAL;
using System;
using System.Collections.Generic;
using HoverTree.Model;
using System.Data;
using System.Data.SqlClient;
using HoverTree.DBTools;

namespace HoverTree.SQLServerDAL
{
    public class HvtImageSet : IImageSet
    {
        public string Add(ImageSetInfo info)
        {
            IDataParameter[] m_parameters = new SqlParameter[7];

            m_parameters[0] = new SqlParameter("@hvtname", SqlDbType.NVarChar, 50);
            m_parameters[1] = new SqlParameter("@hvtfatherfolder", SqlDbType.NVarChar, 50);
            m_parameters[2] = new SqlParameter("@hvttitle", SqlDbType.NVarChar, 500);
            m_parameters[3] = new SqlParameter("@hvtCreateDate", SqlDbType.DateTime);
            m_parameters[4] = new SqlParameter("@hvtisshow", SqlDbType.Bit);
            m_parameters[5] = new SqlParameter("@hvtistop", SqlDbType.Bit);
            m_parameters[6] = new SqlParameter("@hvtLink", SqlDbType.NVarChar, 500);

            m_parameters[0].Value = info.HvtName;
            m_parameters[1].Value = info.HvtfatherFolder;
            m_parameters[2].Value = info.HvtTitle;
            m_parameters[3].Value = info.HvtCreateDate;
            m_parameters[4].Value = info.HvtisShow;
            m_parameters[5].Value = info.HvtIsTop;
            m_parameters[6].Value = info.HvtLink;

            int m_returnCount;

            //执行结果等于1表示添加成功
            m_returnCount = KeleyiSQLHelper.RunProcedureWithReturn("Sp_HoverTree_ImageSet_Add", m_parameters);

            if (m_returnCount == 1)
                return "";
            else
                return "添加失败 ";
        }

        public string Alter(ImageSetInfo info)
        {
            IDataParameter[] m_parameters = new SqlParameter[6];

            m_parameters[0] = new SqlParameter("@hvtid", SqlDbType.Int);
            m_parameters[1] = new SqlParameter("@hvtname", SqlDbType.NVarChar, 50);
            m_parameters[2] = new SqlParameter("@hvttitle", SqlDbType.NVarChar, 500);
            m_parameters[3] = new SqlParameter("@hvtlink", SqlDbType.NVarChar, 500);
            m_parameters[4] = new SqlParameter("@hvtisshow", SqlDbType.Bit);
            m_parameters[5] = new SqlParameter("@hvtistop", SqlDbType.Bit);


            m_parameters[0].Value = info.HvtID;
            m_parameters[1].Value = info.HvtName;
            m_parameters[2].Value = info.HvtTitle;
            m_parameters[3].Value = info.HvtLink;
            m_parameters[4].Value = info.HvtisShow;
            m_parameters[5].Value = info.HvtIsTop;

            int m_returnCount;

            //执行结果等于1表示添加成功
            m_returnCount = KeleyiSQLHelper.RunProcedureWithReturn("Sp_HoverTree_ImageSet_Alter", m_parameters);

            if (m_returnCount == 1)
                return "";
            else
                return "修改失败 ";

        }

        public string Delete(int hvtid)
        {
            IDataParameter[] m_parameters = new SqlParameter[1];
            m_parameters[0] = new SqlParameter("@hvtID", SqlDbType.Int);
            m_parameters[0].Value = hvtid;

            int m_returnCount;

            //执行结果等于1表示添加成功
            m_returnCount = KeleyiSQLHelper.RunProcedureWithReturn("Sp_HoverTree_ImageSet_Delete", m_parameters);

            if (m_returnCount == 1)
                return "";
            else
                return "删除失败,如果图集中有图片则无法删除。 ";
        }

        public ImageSetInfo Get(ImageSetSelect select)
        {
            if (select.HvtID < 1 && select.HvtName.Contains("'"))
                return null;

            IDataParameter[] m_parameters = new SqlParameter[2];
            m_parameters[0] = new SqlParameter("@hvtID", SqlDbType.Int);
            m_parameters[0].Value = select.HvtID;
            m_parameters[1] = new SqlParameter("@hvtname", SqlDbType.NVarChar, 50);
            m_parameters[1].Value = select.HvtName;

            DataRow m_dr;
            m_dr = KeleyiSQLHelper.RunProcedureDataRow("Sp_HoverTree_ImageSet_GET", m_parameters);

            if (m_dr == null)
                return null;

            return ConvertDataRowToInfo(m_dr);
        }

        public List<ImageSetInfo> HvtPageQuery(int sortType, int pageSize, int pageIndex, out int totalCount, out int totalPageCount)
        {
            throw new NotImplementedException();
        }

        public List<ImageSetInfo> HvtPageQuery(ImageSetSelect select, int sortType, int pageSize, int pageIndex, out int totalCount, out int totalPageCount)
        {
            string m_where = " 1=1 ";
            if (select.HvtisShow == 1)
                m_where = m_where + " and hvtisshow=1 ";
            else if (select.HvtisShow == 0)
                m_where = m_where + " and hvtisshow=0 ";

            if (select.HvtIsTop == 1)
                m_where = m_where + " and HvtIsTop=1 ";
            else if (select.HvtIsTop == 0)
                m_where = m_where + " and HvtIsTop=0 ";

            DataTable m_dt = KeleyiSQLHelper.HvtPageQuery("hovert" + "ree_image_set", "*", "hvtid", m_where
                , "  hvtId desc  ", sortType, pageSize, pageIndex, 0, out totalCount, out totalPageCount);

            return ConvertToList(m_dt);
        }

        ImageSetInfo ConvertDataRowToInfo(DataRow row)
        {
            ImageSetInfo m_info = new ImageSetInfo();
            m_info.HvtID = Convert.ToInt32(row["hvtid"]);
            m_info.HvtName = row["hvtname"].ToString();
            m_info.HvtfatherFolder = row["HvtfatherFolder"].ToString();
            m_info.HvtTitle = row["HvtTitle"].ToString();
            m_info.HvtCreateDate = Convert.ToDateTime(row["HvtCreateDate"]);
            m_info.HvtisShow = Convert.ToBoolean(row["HvtisShow"]);
            m_info.HvtIsTop = Convert.ToBoolean(row["HvtIsTop"]);
            m_info.HvtLink = row["HvtLink"].ToString();
            m_info.HvtCover = row["hvtcover"].ToString();
            return m_info;
        }

        List<ImageSetInfo> ConvertToList(DataTable table)
        {
            List<ImageSetInfo> m_list = new List<ImageSetInfo>();
            if (table == null)
                return null;
            int m_rows = table.Rows.Count;
            if (m_rows == 0)
                return m_list;

            for (int i = 0; i < m_rows; i++)
            {
                ImageSetInfo m_info = new ImageSetInfo();

                m_info = ConvertDataRowToInfo(table.Rows[i]);
                m_list.Add(m_info);
            }

            return m_list;
        }

    }
}